package com.evision.webcrawler.configure;

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.boot.autoconfigure.jms.DefaultJmsListenerContainerFactoryConfigurer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.ConnectionFactory;
import javax.jms.Queue;
import javax.jms.Topic;

/**
 * 消息队列配置
 */
@Configuration
public class JmsConfigure {
    public static final String SEED_TOPIC = "com.evision.webcrawler.SEEDTOPIC";
    public static final String CHECK_QUEUE = "com.evision.webcrawler.CHECKQUEUE";

    @Bean
    public Topic seedTopic() {
        return new ActiveMQTopic(JmsConfigure.SEED_TOPIC);
    }

    @Bean
    public Queue checkQueue() {
        return new ActiveMQQueue(JmsConfigure.CHECK_QUEUE);
    }

    @Bean
    public JmsListenerContainerFactory topicListenerFactory(ConnectionFactory connectionFactory,
                                                            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        factory.setPubSubDomain(true);
        return factory;
    }

    @Bean
    public JmsListenerContainerFactory queueListenerFactory(ConnectionFactory connectionFactory,
                                                            DefaultJmsListenerContainerFactoryConfigurer configurer) {
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }
}
